<?php

namespace app\common\helper;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use think\Exception;

class SkinHelper
{
    protected $api_id;
    protected $api_key;
    protected $base_url;
    protected $body;
    protected $type;
    public function __construct()
    {
        $config = cache('config');
        $this->api_id = $config['api_id'];
        $this->api_key = $config['api_key'];
        $this->base_url = "https://api.yimei.ai/v2/api/face/analysis/";
    }
    
    public function setBody($image)
    {
        // 色斑1 纹理1  水分1   痘痘1 痘印1 黑头1      皱纹1      毛孔1
        // 	2  	1024 4096 65536     32768   8192 	4
        
        $this->type = 2+1024+4096+65536+32768+8192+4;
        
        if (substr($image,0,4) !== 'http') {
            $image = request()->domain() . $image;
        }
        $this->body = [
            "image" => $image
        ];
        return $this;
    }
    
    public function send()
    {
        try {
            $client = new Client();
            $result = $client->post($this->base_url . $this->type,[
                "headers" => [
                    "Authorization" => "Basic " . base64_encode($this->api_id .":". $this->api_key)
                ],
                "form_params" => $this->body
            ]);
            return json_decode($result->getBody(),true);
        } catch (Exception | GuzzleException $exception) {
            throwError($exception->getMessage());
        }

    }
}